package stubs
{
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import mx.core.EmbeddedFontRegistry;
	import mx.core.IFlexModuleFactory;
	import mx.core.RSLData;
	
	import spark.components.Button;
	import spark.core.SpriteVisualElement; EmbeddedFontRegistry;
	// Need this for some other stuff... something in UIComponent
	// sigh...
	import mx.managers.BrowserManagerImpl; BrowserManagerImpl;
	// A friend of the guy above...
	import mx.managers.HistoryManagerImpl; HistoryManagerImpl;
	// Yet another masterpiece...
	import mx.managers.PopUpManagerImpl; PopUpManagerImpl;
	// Another piece of art that hopefully gets deleted some time...
	import mx.managers.DragManagerImpl; DragManagerImpl;
	// Moar abominations...
	import mx.core.TextFieldFactory;
	// even moar...
	import mx.styles.StyleManagerImpl; StyleManagerImpl;
	// So that we import this poop too...
_ApplicationStub_Styles;
	// Not sure if this is needed, this was a bizarre finding...
	import mx.managers.systemClasses.ChildManager; ChildManager;
	
	import mx.core.UIComponentGlobals;
	import mx.managers.LayoutManager;
	import spark.skins.spark.ButtonSkin;
	import mx.events.FlexEvent;
	import mx.core.FlexGlobals;

 TextFieldFactory;
	
	[SWF(width="800", height="600", scriptTimeLimit="15", 
		frameRate="30", backgroundColor="0x3E2F1B")]
	[Frame(factoryClass="stubs.SystemManagerStub")]
	[Frame(extraFrame="stubs.SystemManagerStub")]
	// turn this off for now
	//[Mixin]	
	
	public class ApplicationStub extends SpriteVisualElement 
		implements IFlexModuleFactory
	{
		public namespace mx_internal = 
			"http://www.adobe.com/2006/flex/mx/internal";
		
		use namespace mx_internal;
		// Another masterpiece...
		UIComponentGlobals.layoutManager = LayoutManager.getInstance();
		UIComponentGlobals.layoutManager.usePhasedInstantiation = true;
		
		public function ApplicationStub()
		{
			super();
			FlexGlobals.topLevelApplication = this;
			super.addEventListener(
				Event.ADDED_TO_STAGE, this.addedToStageHandler);
			//throw "I'm here";
			super.addEventListener(
				FlexEvent.APPLICATION_COMPLETE, 
				this.applicationCompleteHandler);
		}
		
		private function applicationCompleteHandler(event:FlexEvent):void
		{
			// We've lost something related to skinning
			// don't know what exactly
			var button:Button = new Button();
			button.document = this;
			
			// only UIComponents
			//button.fontContext = moduleFactory;
			
			button.parentChanged(this);
			
			button.nestLevel = 1;
			
			button.regenerateStyleCache(true);
			
			// may be important
			//StyleProtoChain.initTextField(IUITextField(child));
			
			button.styleChanged(null);
			
			button.notifyStyleChangeInChildren(null, true);
			
			// only UIComponents
			//button.initThemeColor();
			
			button.stylesInitialized();
			//button.moduleFactory = this;
			//trace("setStyle skinClass");
			//button.setStyle("skinClass", ButtonSkin);
			// What would you do without adjusting the Japanese fonts on
			// a button with English text only in en_US locale? huh?
			//button.setStyle("justificationStyle", "prioritizeLeastAdjustment");
			//button.styleChanged("skinClass");
			//button.regenerateStyleCache(true);
			button.label = "test";
			button.width = button.height = 100;
			//button.invalidateSkinState();
			//button.validateDisplayList();
			//button.validateNow();
			super.addChild(button);
		}
		
		private function addedToStageHandler(event:Event):void
		{
			// var button:Button = new Button();
			// button.setStyle("skinClass", ButtonSkin);
			// button.label = "test";
			// button.width = button.height = 100;
			// super.addChild(button);
		}
		
		// [Mixin] support
		public static function init(obj:Object):void
		{
			//trace("mixin", obj);
			// this must be the system manager
		}
		
		/* INTERFACE mx.core.IFlexModuleFactory */
		// this interface is twice as big as it was half a year ago
		// most of the new methods aren't needed.
		// not that old were any better...
		public function get allowDomainsInNewRSLs():Boolean { return false; }
		
		public function set allowDomainsInNewRSLs(value:Boolean):void { }
		
		public function get allowInsecureDomainsInNewRSLs():Boolean { return false; }
		
		public function set allowInsecureDomainsInNewRSLs(value:Boolean):void { }
		
		public function get preloadedRSLs():Dictionary { return null; }
		
		public function info():Object { return null; }
		
		public function create(... parameters):Object { return null; }
		
		// Rage...
		public function addPreloadedRSL(loaderInfo:LoaderInfo, rsl:Vector.<RSLData>):void { }
		
		public function registerImplementation(a:String, b:Object):void { }
		
		public function getImplementation(a:String):Object { return null; }
		
		public function allowDomain(... domains):void {  }
		
		public function allowInsecureDomain(... domains):void {  }
		
		public function callInContext(fn:Function, thisArg:Object,
			argArray:Array, returns:Boolean = true):* { return null; }
	}
}